/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package chelydra.sharelydra.client.network;

import chelydra.sharelydra.client.Core;
import chelydra.sharelydra.client.database.DatabaseManager;
import chelydra.sharelydra.client.file.FriendFilesData;
import chelydra.sharelydra.client.file.ListFriendFilesData;
import chelydra.sharelydra.client.friend.Notification;
import chelydra.sharelydra.client.message.GetFileList;
import chelydra.sharelydra.client.message.SendFileList;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Benjamin
 */
public class GetFileListThread extends Thread {

    InetAddress IPDestInet;
    String IPDest;
    String friendName;
    int portDest;

    String reply;


    public GetFileListThread(String IPDest,int portDest, String friendName) throws UnknownHostException, IOException{

        this.IPDestInet = InetAddress.getByName(IPDest);
        this.portDest=portDest;
        this.friendName = friendName;
        
    }

       public void run(){

        GetFileList mes = new GetFileList();
        Socket s;
        try {
            System.out.println("GetFileList Thread launched");
            System.out.println("Dest" + this.IPDestInet + "  PortDest  " + this.portDest );
            s = new Socket(this.IPDestInet, this.portDest);
            OutputStream os = s.getOutputStream();
            InputStream is = s.getInputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os);
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            osw.write(mes.toString(),0,mes.toString().length());
            osw.flush();
            System.out.println("GetFile list send to: "+this.IPDestInet.toString()+" : "+this.portDest);
            while (br.ready() == false ){
            }
            reply=br.readLine();
            System.out.println("reply "+reply);
            //Reply processing
            SendFileList mesReply= new SendFileList();
            mesReply.fromString(reply);
            System.out.println("Initializator recieved this message");
            System.out.println(reply.toString());
            //to do traitement du fichier xml de la  liste des fichiers.
            String list = mesReply.getList();
            System.out.println("I display list");
            System.out.println(list);
            while(  list != null && list.contains("<file>")){
                String line = list.substring(list.indexOf("<file>")+6,list.indexOf("</file>"));
                System.out.println("Line : "+line);
                list = list.substring(list.indexOf("</file>")+5,list.length());
                String fileID = line.substring(line.indexOf("<fileID>")+8,line.indexOf("</fileID>"));
                String fileName = line.substring(line.indexOf("<fileName>")+10,line.indexOf("</fileName>"));
                String fileSize = line.substring(line.indexOf("<fileSize>")+10,line.indexOf("</fileSize>"));
                String filePath = line.substring(line.indexOf("<filePath>")+10,line.indexOf("</filePath>"));
                FriendFilesData ffd = new FriendFilesData(Integer.parseInt(fileID),fileName,
                        filePath,Integer.parseInt(fileSize),s.getInetAddress(),this.friendName);
                ListFriendFilesData.List.add(ffd);
            }
            Core.getApplication().getView().updateFriendFiles();
            DatabaseManager dbm = Core.getApplication().getDatabaseManager();
    		Notification newNote = new Notification(Notification.LIST_FILE_DOWNLOADED,this.friendName);
	    	dbm.addNewNotification(newNote);
		    Core.getApplication().getView().updateNotifications();
            osw.close();

        } catch (IOException ex) {
            Logger.getLogger(GetFileListThread.class.getName()).log(Level.SEVERE, null, ex);
        }
        

    }
    
}
